<!DOCTYPE html>
<html>
<head>
	<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui"/>
	<meta name="apple-mobile-web-app-capable" content="yes"/>
	<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
	<meta name="format-detection" content="telephone=no, email=no"/>
	<meta charset="UTF-8">
	<title>购物车</title>
	<link rel="stylesheet" href="themes/css/core.css">
	<link rel="stylesheet" href="themes/css/icon.css">
	<link rel="stylesheet" href="themes/css/home.css">
	<link rel="icon" type="image/x-icon" href="favicon.ico">
	<link href="iTunesArtwork@2x.png" sizes="114x114" rel="apple-touch-icon-precomposed">
</head>
<body>
<div id="el">
	<header class="aui-header-default aui-header-fixed ">
		<a href="javascript:history.back(-1)" class="aui-header-item">
			<i class="aui-icon aui-icon-back"></i>
		</a>
		<div class="aui-header-center aui-header-center-clear">
			<div class="aui-header-center-logo">
				<div class="">填写订单</div>
			</div>
		</div>
		<a href="#" class="aui-header-item-icon"   style="min-width:0">
			<i class="aui-icon aui-icon-member"></i>
		</a>
	</header>
	<section class="aui-address-content">
		<div class="aui-address-box">
			<div class="aui-address-box-list">
				<a href="my-addres.html" class="aui-address-box-default">
					<!--默认地址-->
					<ul>
						<li>
							<strong><span v-text="defaultAddress.name"></span> <span v-text="defaultAddress.tel"></span></strong>
							<span class="aui-tag" id="aui-home">常用</span>
							<span class="aui-tag aui-tag-default" id="aui-default">默认</span>
						</li>
						<li>
							<i class="aui-icon aui-icon-address"></i>
							<span v-text="defaultAddress.addr"></span>
						</li>
					</ul>
				</a>
			</div>
		</div>
		<div class="aui-dri"></div>
		<div class="aui-list-product-float-item" v-for="bu in business">
			<div class="aui-car-box-name">
				<h3>
					<!--商店展示-->
					<a href="#" v-text="bu.business_name"></a>
				</h3>
			</div>
			<a href="javascript:;" class="aui-list-product-fl-item" v-for="pro in product">
				<div v-if="pro.business_id==bu.business_id">
					<div class="aui-list-product-fl-img">
						<img :src="pro.product_img" alt="">
					</div>
					<div class="aui-list-product-fl-text">
						<h3 class="aui-list-product-fl-title" v-text="pro.product_desc"></h3>
						<div class="aui-list-product-fl-mes">
							<div>
								<span class="aui-list-product-item-price">
									<em>¥</em>
									<span v-text="pro.promotion_price"></span>
								</span>
								<span class="aui-list-product-item-del-price">
									¥<span v-text="pro.product_price"></span>
								</span>
								&ensp;&ensp;&ensp;&ensp;&ensp;&emsp;&emsp;&emsp;&emsp;&emsp;
								&ensp;&ensp;&ensp;&ensp;&ensp;&emsp;&emsp;&emsp;&emsp;&emsp;
								数量：
								<span v-text="pro.product_num"></span>
							</div>

						</div>
						<div class="aui-list-product-fl-bag">
							<span><img src="themes/img/icon/bag1.png" alt=""></span>
							<span><img src="themes/img/icon/bag2.png" alt=""></span>
						</div>
					</div>
				</div>
			</a>
		</div>
		<div class="aui-address-well">
			<a href="#" class="aui-address-cell aui-fl-arrow">
				<div class="aui-address-cell-bd">支持配送</div>
				<div class="aui-address-cell-ft">
					<p>在线支付</p>
					<p class="aui-address-text"><i class="aui-icon aui-prompt-sm"></i>快递</p>
				</div>
			</a>
			<div class="aui-prompt"><i class="aui-icon aui-prompt-sm"></i>请您做好收货准备。</div>
			<a href="../user/liquanbao_shoppingmall.html" class="aui-address-cell aui-fl-arrow aui-fl-arrow-clear">
				<div class="aui-address-cell-bd">优惠券</div>
				<div class="aui-address-cell-ft" v-if="cupon.cupon_content==null">详情</div>
				<div class="aui-address-cell-ft" v-text="cupon.cupon_content"></div>
			</a>
			<div class="aui-dri"></div>
			<a href="javascript:" class="aui-address-cell aui-fl-arrow aui-fl-arrow-clear">
				<div class="aui-address-cell-bd">成长值</div>
				<div class="aui-address-cell-ft">{{integral_result}}</div>
			</a>
			<div class="aui-dri"></div>
			<a href="#" class="aui-address-cell aui-fl-arrow aui-fl-arrow-clear">
				<div class="aui-address-cell-bd">
					<h3>商品金额</h3>
					<p>运费</p>
				</div>
				<div class="aui-address-cell-ft">
					<span class="aui-red">￥{{total_amount}}</span><br>
					<span class="aui-red">+￥0.00</span>
				</div>
			</a>
		</div>
		<div class="aui-payment-bar">
			<div class="shop-total">
				<span class="aui-red aui-size">实付款: <em>￥{{total_amount}}</em></span>
			</div>
			<!--<a href="Settlement.html" class="settlement">提交订单</a>-->
			<a href="javascript:" class="settlement" @click="commitOrder()" >提交订单</a>
		</div>
	</section>
</div>
	<script src="themes/js/jquery.min.js"></script>
	<script src="themes/js/vue.js"></script>
<script type="text/javascript" >
	var a_num = 0;
	var vue = new Vue({
		el:"#el",
		data:{
			//默认地址
			defaultAddress:[],
			//选中了的购物车商品id
			checks:[],
			//选中了的店铺
			business:[],
			//选中了的商品
			product:[],
			//优惠券页面选中了的优惠券
			cupon:0,
			//成长比例
			integral:[],
		},
		created:function(){
			this.init();
		},
		Mounted:function(){

		},
		methods:{
			init:function(){
				var _this = this;
				//默认地址查询
				$.ajax({
					type:"get",
					url:"/ShoppingMallOrder/getAddressByUserId",
					dataType:"json",
					success:function(da){
						if(da.obj != null){
							_this.defaultAddress = da.obj;
						}else{
							_this.defaultAddress = 0;
						}
					}
				});
				//获取选中了的购物车商品
				$.ajax({
					type:"get",
					url:"/ShoppingMallOrder/getChecks2",
					dataType:"json",
					success:function(da){
						_this.checks = da.obj;
						//商品
						$.ajax({
							type:"get",
							url:"/ShoppingMallCar/getProductByUserId",
							dataType:"json",
							success:function(da){
								for (var i = 0;i<da.li.length;i++){
									for(var j = 0;j < _this.checks.length;j++){
										if(da.li[i].product_id == _this.checks[j]){
											_this.product.push(da.li[i]);
											break;
										}
									}

								}
								//店铺
								$.ajax({
									type:"get",
									url:"/ShoppingMallCar/getBusinessByUserId",
									dataType:"json",
									success:function(da){
										for(var i = 0;i < da.li.length;i++){
											for(var j = 0;j < _this.product.length;j++){
												if(da.li[i].business_id == _this.product[j].business_id){
													_this.business.push(da.li[i]);
													break;
												}
											}
										}
									}
								});
							}
						});
					}
				});
				//获取优惠券id，查询
				$.ajax({
					type:"get",
					url:"/weixin/user/selectliquanbao2",
					dataType:"json",
					success:function(da){
						if(da.obj!=null){
							$.ajax({
								url:"/weixin/user/queryUserCupon2",
								data:{},
								dataType:"json",
								Type:"get",
								success:function (data) {
									if(data != null && data != ""){
										for(var i = 0;i < data.li.length;i++){
											if(data.li[i].uc_id == da.obj){
												_this.cupon=data.li[i];
											}
										}
									}

								}
							})

						}

					}
				});
				//查询用户成长值比例，展示成长值
				$.ajax({
					type:"get",
					url:"/ShoppingMallOrder/getIntegral",
					dataType:"json",
					success:function(da){
						_this.integral = da.obj.ratio;
					}
				});

			},
			commitOrder:function(){

					if (a_num == 0){
						var _this = this;
						// alert(_this.cupon);
						//创建总订单
						if(_this.cupon != 0){
						$.ajax({
							type:"get",
							url:"/ShoppingMallOrder/insertAllOrder",
							data:{"userAddressId":_this.defaultAddress.userAddressId,
								"alltotalprice":_this.total_amount,
								"userId":_this.defaultAddress.userId,
								"userCuponId":_this.cupon.cupon_id,
								"allorderOriginalprice":parseFloat(_this.total_amount)+parseFloat(_this.cupon.cupon_money),},
							async: false,
							dataType:"json",
							success:function(da0){
								// alert("新增总订单成功");
								// alert(da0.obj);
								//》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
								//创建子订单
								for(var i = 0;i < _this.business.length;i++){
									var userId = _this.defaultAddress.userId;
									var myorderStatus = 0;
									var myorderTotalPrice = 0;
									for(j = 0;j < _this.product.length;j++){
										if(_this.product[j].business_id == _this.business[i].business_id){
											myorderTotalPrice += _this.product[j].product_num*_this.product[j].promotion_price;
										}
									}
									var allorderId = da0.obj;
									var bussinessId = _this.business[i].business_id;
									$.ajax({
										type:"get",
										url:"/ShoppingMallOrder/insertChildOrder",
										data:{"userId":userId,
											"myorderStatus":myorderStatus,
											"myorderTotalPrice":myorderTotalPrice.toFixed(2),
											"allorderId":allorderId,
											"bussinessId":bussinessId,},
										async: false,
										dataType:"json",
										success:function(da1){
											// alert("新增子订单成功");
											// alert(da1.obj);
											//》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
											//根据子订单生成订单项
											// alert(1);
											for(k = 0;k < _this.product.length;k++){
												if(_this.product[k].business_id == bussinessId){
													// alert(123321);
													//每一个商品
													var productId = _this.product[k].product_id;
													var productNum = _this.product[k].product_num;
													var productPrice = _this.product[k].promotion_price;
													var totalPrice = (productPrice*productNum).toFixed(2);
													var myorderId = da1.obj;
													var odIntegral = parseInt(totalPrice*_this.integral);
													$.ajax({
														type:"get",
														url:"/ShoppingMallOrder/insertOrderDetail",
														data:{"productId":productId,
															"productNum":productNum,
															"productPrice":productPrice.toFixed(2),
															"totalPrice":totalPrice,
															"myorderId":myorderId,
															"odIntegral":odIntegral,},
														async: false,
														dataType:"json",
														success:function(da2){
															// alert(da2.obj);
														}
													});

												}
											}

										}
									});
								}

							}
						});
					}else{

							$.ajax({
								type:"get",
								url:"/ShoppingMallOrder/insertAllOrder",
								data:{"userAddressId":_this.defaultAddress.userAddressId,
									"alltotalprice":_this.total_amount,
									"userId":_this.defaultAddress.userId,
									"userCuponId":0,
									"allorderOriginalprice":parseFloat(_this.total_amount),},
								async: false,
								dataType:"json",
								success:function(da0){
									// alert("新增总订单成功");
									// alert(da0.obj);
									//》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
									//创建子订单
									for(var i = 0;i < _this.business.length;i++){
										var userId = _this.defaultAddress.userId;
										var myorderStatus = 0;
										var myorderTotalPrice = 0;
										for(j = 0;j < _this.product.length;j++){
											if(_this.product[j].business_id == _this.business[i].business_id){
												myorderTotalPrice += _this.product[j].product_num*_this.product[j].promotion_price;
											}
										}
										var allorderId = da0.obj;
										var bussinessId = _this.business[i].business_id;
										$.ajax({
											type:"get",
											url:"/ShoppingMallOrder/insertChildOrder",
											data:{"userId":userId,
												"myorderStatus":myorderStatus,
												"myorderTotalPrice":myorderTotalPrice.toFixed(2),
												"allorderId":allorderId,
												"bussinessId":bussinessId,},
											async: false,
											dataType:"json",
											success:function(da1){
												// alert("新增子订单成功");
												// alert(da1.obj);
												//》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
												//根据子订单生成订单项
												// alert(1);
												for(k = 0;k < _this.product.length;k++){
													if(_this.product[k].business_id == bussinessId){
														// alert(123321);
														//每一个商品
														var productId = _this.product[k].product_id;
														var productNum = _this.product[k].product_num;
														var productPrice = _this.product[k].promotion_price;
														var totalPrice = (productPrice*productNum).toFixed(2);
														var myorderId = da1.obj;
														var odIntegral = parseInt(totalPrice*_this.integral);
														$.ajax({
															type:"get",
															url:"/ShoppingMallOrder/insertOrderDetail",
															data:{"productId":productId,
																"productNum":productNum,
																"productPrice":productPrice.toFixed(2),
																"totalPrice":totalPrice,
																"myorderId":myorderId,
																"odIntegral":odIntegral,},
															async: false,
															dataType:"json",
															success:function(da2){
																// alert(da2.obj);
															}
														});

													}
												}

											}
										});
									}

								}
							});
						}
						// alert(this.total_amount);
						//存储最终价格数据，支付成功界面获取，后期可以删除此ajax
						$.ajax({
							type:"get",
							url:"/ShoppingMallOrder/savetotal_amount",
							data:{"total_amount":this.total_amount},
							async: false,
							success:function(da){
							}
						});
						//http://wchuan.mynatapp.cc/alipay/pay?total_amount=11
						// location.href=window.location.protocol+"//"+window.location.host+"/alipay/pay?total_amount="+_this.total_amount;
						// location.href=window.location.protocol+"//"+window.location.host+"/weixin/alipay3?total_amount="+_this.total_amount;
						// location.href= window.location.protocol +"//"+window.location.host+"/weixin/alipay3?total_amount="+_this.total_amount;

						//调用支付
						$.ajax({
							url:"/weixin/buyOrderPay",
							// data:{"Money":_this.total_amount},
							data:{"Money":0.01},
							dataType:"json",
							Type:"get",
							success:function (data) {

								function onBridgeReady() {
									WeixinJSBridge.invoke(
											'getBrandWCPayRequest', {
												"appId": data.obj.appId,     //公众号名称，由商户传入
												"timeStamp": data.obj.timeStamp,       //时间戳，自1970年以来的秒数
												"nonceStr": data.obj.nonceStr, //随机串
												"package": data.obj.package,
												"signType": "MD5",         //微信签名方式：
												"paySign": data.obj.paySign //微信签名
											},
											function (res) {
												if (res.err_msg == "get_brand_wcpay_request:ok") {
													// 使用以上方式判断前端返回,微信团队郑重提示：
													//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
													window.location.href = "http://zhouhao1.ngrok2.xiaomiqiu.cn/weixin/wechatpay/cg.html";
												}
											});
								}

								if (typeof WeixinJSBridge == "undefined") {
									if (document.addEventListener) {
										document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
									} else if (document.attachEvent) {
										document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
										document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
									}
								} else {
									onBridgeReady();
								}

							}
						});

					}
					a_num++;
			}
		},
		computed:{
			//总价
			total_amount:function(){
				var total = 0;
				for(var i=0;i<this.product.length;i++){
					total += this.product[i].promotion_price*this.product[i].product_num;
				}
				if(this.cupon.cupon_money>0){
					//有优惠券，减去优惠券、判断是否大于0、保留两位小数
					return (total-this.cupon.cupon_money).toFixed(2)>0?(total-this.cupon.cupon_money).toFixed(2):0.00;
				}else{
					//没有优惠券，不用再减去优惠券的金额
					return total.toFixed(2);
				}

			},
			//最终成长值
			integral_result:function(){
				return parseInt(this.integral*this.total_amount);
			}
		},



	});
</script>

</body>
</html>